<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Multi threading in CANlib</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_example_c_threads.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Multi threading in CANlib </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>samples\example\c\thread.c</b> </p><div class="fragment"><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">** This software is furnished as Redistributable under the Kvaser Software Licence</span></div><div class="line"><span class="comment">** https://www.kvaser.com/canlib-webhelp/page_license_and_copyright.html</span></div><div class="line"><span class="comment"></span></div><div class="line"><span class="comment">** Description:</span></div><div class="line"><span class="comment">** This is a multithreaded program that uses CANLIB.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;memory.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;windows.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;conio.h&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="canlib_8h.html">canlib.h</a>&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef __BORLANDC__</span></div><div class="line"><span class="preprocessor">#pragma argsused</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"><span class="keywordtype">void</span> Usage(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">  printf(<span class="stringliteral">&quot;\nCANLIB Thread Test 4\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;\nUsage: thread [flags]\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -aX         Use channel number X as source\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -bY         Use channel number Y as destination\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   (note: X must be different from Y. Default X=0, Y=1)\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -B&lt;value&gt;   Set the bitrate. Value is any of 1000,500,250,125.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;               Default bitrate is 125 kbit/s.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -v          Be verbose.\n&quot;</span>);</div><div class="line">  printf(<span class="stringliteral">&quot;   -Lnnn       Run nnn \&quot;loops\&quot;. Default is 500.\n&quot;</span>);</div><div class="line"></div><div class="line">  exit(1);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Global variables for the command-line options.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="keywordtype">int</span> Bitrate = <a class="code" href="canlib_8h.html#a29a9aafde6e861b4fffb72ad124aeed1">canBITRATE_125K</a>;   <span class="comment">//</span></div><div class="line"><span class="keywordtype">int</span> Verbose = 0;</div><div class="line"><span class="keywordtype">int</span> MaxLoops = 500;</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Check a status code and issue an error message if the code</span></div><div class="line"><span class="comment">// isn&#39;t canOK.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="keywordtype">void</span> Check(<span class="keywordtype">char</span>* <span class="keywordtype">id</span>, <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat)</div><div class="line">{</div><div class="line">  <span class="keywordflow">if</span> (stat != <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">    <span class="keywordtype">char</span> buf[50];</div><div class="line">    buf[0] = <span class="charliteral">&#39;\0&#39;</span>;</div><div class="line">    <a class="code" href="group__can__general.html#ga01a7a415c95c579750bcdd95a1d245c4">canGetErrorText</a>(stat, buf, <span class="keyword">sizeof</span>(buf));</div><div class="line">    printf(<span class="stringliteral">&quot;%s: failed, stat=%d (%s)\n&quot;</span>, <span class="keywordtype">id</span>, (<span class="keywordtype">int</span>)stat, buf);</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Setup a CAN controller.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> InitCtrl(<span class="keywordtype">int</span> ctrl)</div><div class="line">{</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a> stat;</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line"></div><div class="line">  hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(ctrl, 0x0020);</div><div class="line">  <span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">    printf(<span class="stringliteral">&quot;canOpenChannel failed, stat=%d, ctrl=%d\n&quot;</span>, hnd, ctrl);</div><div class="line">    exit(0);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Using our new shiny handle, we specify the baud rate</span></div><div class="line">  <span class="comment">// using one of the convenient canBITRATE_xxx constants.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// The bit layout is in depth discussed in most CAN</span></div><div class="line">  <span class="comment">// controller data sheets, and on the web at</span></div><div class="line">  <span class="comment">// http://www.kvaser.se.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga7eb8c2e92cfae57e7ec5031818524301">canSetBusParams</a>(hnd, Bitrate, 0, 0, 0, 0, 0);</div><div class="line">  <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">    printf(<span class="stringliteral">&quot;canSetBusParams failed, stat=%d\n&quot;</span>, stat);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// Then we start the ball rolling.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  stat = <a class="code" href="group___c_a_n.html#ga99c7c99cc71580f8099a1407f4f9ea1a">canBusOn</a>(hnd);</div><div class="line">  <span class="keywordflow">if</span> (stat &lt; 0) {</div><div class="line">    printf(<span class="stringliteral">&quot;canBusOn failed, stat=%d\n&quot;</span>, stat);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Return the handle; our caller will need it for</span></div><div class="line">  <span class="comment">// further exercising the CAN controller.</span></div><div class="line">  <span class="keywordflow">return</span> hnd;</div><div class="line">}</div><div class="line"></div><div class="line">CRITICAL_SECTION cs;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">long</span> TimeToLeave;</div><div class="line"><span class="keyword">static</span> <span class="keywordtype">long</span> TotalSent[4];</div><div class="line"><span class="keyword">static</span> <span class="keywordtype">long</span> TotalReceived[4];</div><div class="line"></div><div class="line"><span class="preprocessor">#define NR_MSGS 3</span></div><div class="line"><span class="preprocessor">#define SRC_ID  300</span></div><div class="line"><span class="preprocessor">#define P_NAP   50</span></div><div class="line"><span class="preprocessor">#define C_NAP   0</span></div><div class="line"></div><div class="line"><a class="code" href="kvmlib_8h.html#aa8c0374618b33785ccb02f74bcfebc46">HANDLE</a> events[4];</div><div class="line"><span class="keywordtype">int</span> channels[4];</div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> <a class="code" href="kva_db_lib_8h.html#a9aa60e1ead64be77ad551e745cbfd4d3">WINAPI</a> SendAndReceive(PVOID p)</div><div class="line">{</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a>   hnd;</div><div class="line">  <span class="keywordtype">long</span>        id_read;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>   stat;</div><div class="line">  <a class="code" href="linlib_8h.html#a4ae1dab0fb4b072a66584546209e7d58">BYTE</a>        msg[8];</div><div class="line">  <span class="keywordtype">int</span>         i, count;</div><div class="line">  ULONG       idx;</div><div class="line"></div><div class="line">  idx = PtrToUlong(p);</div><div class="line"></div><div class="line">  hnd = InitCtrl(channels[idx]);</div><div class="line">  TotalSent[idx] = 0;</div><div class="line">  count = 0;</div><div class="line">  WaitForSingleObject(events[idx], INFINITE);</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (TRUE) {</div><div class="line">    <span class="keywordtype">long</span> id;</div><div class="line"></div><div class="line">    <span class="keywordtype">id</span> = SRC_ID + idx;</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> (i=0; i&lt;NR_MSGS; i++) {</div><div class="line">      <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> dlc, flags;</div><div class="line">      <span class="comment">// Send one..</span></div><div class="line">      <span class="comment">//memcpy(msg, &amp;i, sizeof(i));</span></div><div class="line">      memcpy(msg, &amp;count, <span class="keyword">sizeof</span>(count));</div><div class="line">      count++;</div><div class="line">      stat = <a class="code" href="group___c_a_n.html#ga62c185329d6741c90102511e2f37983e">canWrite</a>(hnd, <span class="keywordtype">id</span>, msg, 8, 0);</div><div class="line">      Check(<span class="stringliteral">&quot;canWrite A&quot;</span>, stat);</div><div class="line">      TotalSent[idx]++;</div><div class="line"></div><div class="line">      <span class="comment">// And receive all that is pending..</span></div><div class="line">      <span class="keywordflow">do</span> {</div><div class="line">        stat = <a class="code" href="group___c_a_n.html#ga157d49a9343dea68ac953145e02266d8">canRead</a>(hnd, &amp;id_read, NULL, &amp;dlc, &amp;flags, NULL);</div><div class="line">        <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">          <span class="keywordflow">if</span> (Verbose &gt; 1) printf(<span class="stringliteral">&quot;R&quot;</span>);</div><div class="line">          TotalReceived[idx]++;</div><div class="line">          <span class="comment">// if (id_read != SRC_ID) printf(&quot;Id=%d dlc=%d flags=0x%x\n&quot;, id_read, dlc, flags);</span></div><div class="line">          <span class="keywordflow">if</span> (flags &amp; canMSG_ERROR_FRAME) printf(<span class="stringliteral">&quot;x&quot;</span>);</div><div class="line">        }</div><div class="line">      } <span class="keywordflow">while</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>);</div><div class="line">    }</div><div class="line">    stat = <a class="code" href="group___c_a_n.html#ga304cb3a7bc2874c1f8ad361a911bcd5f">canWriteSync</a>(hnd, 1000);</div><div class="line">    Check(<span class="stringliteral">&quot;canWriteSync A&quot;</span>, stat);</div><div class="line">    Sleep(P_NAP);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (TimeToLeave) <span class="keywordflow">break</span>;</div><div class="line">  }</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> <a class="code" href="kva_db_lib_8h.html#a9aa60e1ead64be77ad551e745cbfd4d3">WINAPI</a> ReadOne(PVOID p)</div><div class="line">{</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a>   hnd;</div><div class="line">  <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7">canStatus</a>   stat;</div><div class="line">  ULONG       idx;</div><div class="line"></div><div class="line">  idx = PtrToUlong(p);</div><div class="line"></div><div class="line">  hnd = InitCtrl(channels[idx]);</div><div class="line">  TotalReceived[idx] = 0;</div><div class="line">  WaitForSingleObject(events[idx], INFINITE);</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (TRUE) {</div><div class="line"></div><div class="line">    <span class="comment">//</span></div><div class="line">    <span class="comment">// Call canReadSyncSpecific to wait for the message(s) that the other threads</span></div><div class="line">    <span class="comment">// are sending back and forth, then call canRead to consume the messages.</span></div><div class="line">    <span class="comment">//</span></div><div class="line">    <span class="keywordflow">do</span> {</div><div class="line">      stat = <a class="code" href="group___c_a_n.html#ga4b21a0e2547c1435c378d13e6803acef">canReadSyncSpecific</a>(hnd, SRC_ID, 500);</div><div class="line">      <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        <span class="keywordflow">if</span> (Verbose &gt; 1) printf(<span class="stringliteral">&quot;r&quot;</span>);</div><div class="line">      } <span class="keywordflow">else</span> {</div><div class="line">        <span class="keywordflow">if</span> (Verbose &gt; 1) printf(<span class="stringliteral">&quot;T&quot;</span>);</div><div class="line">      }</div><div class="line">      <span class="keywordflow">while</span> (<a class="code" href="group___c_a_n.html#ga157d49a9343dea68ac953145e02266d8">canRead</a>(hnd, NULL, NULL, NULL, NULL, NULL) == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        TotalReceived[idx]++;</div><div class="line">      }</div><div class="line">    } <span class="keywordflow">while</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>);</div><div class="line"></div><div class="line">    <span class="comment">//</span></div><div class="line">    <span class="comment">// Call canReadSyncSpecific again to wait for a non-existent id - this time</span></div><div class="line">    <span class="comment">// we expect a timeout.</span></div><div class="line">    <span class="comment">//</span></div><div class="line">    <span class="keywordflow">do</span> {</div><div class="line">      stat = <a class="code" href="group___c_a_n.html#ga4b21a0e2547c1435c378d13e6803acef">canReadSyncSpecific</a>(hnd, SRC_ID+5, 50);</div><div class="line">      <span class="keywordflow">if</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">        printf(<span class="stringliteral">&quot;Whoops, canReadSyncSpecific failed&quot;</span>);</div><div class="line">      }</div><div class="line">    } <span class="keywordflow">while</span> (stat == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (TimeToLeave) <span class="keywordflow">break</span>;</div><div class="line">    Sleep(C_NAP);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment">// Just consume all messages on the bus.</span></div><div class="line"><span class="comment">//</span></div><div class="line"><a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> <a class="code" href="kva_db_lib_8h.html#a9aa60e1ead64be77ad551e745cbfd4d3">WINAPI</a> Sink(PVOID p)</div><div class="line">{</div><div class="line">  <a class="code" href="canlib_8h.html#ae3d1b041d62207d5336f93c089cd5b65">canHandle</a> hnd;</div><div class="line">  ULONG     idx;</div><div class="line"></div><div class="line">  idx = PtrToUlong(p);</div><div class="line">  hnd = InitCtrl(channels[idx]);</div><div class="line">  TotalReceived[idx] = 0;</div><div class="line">  WaitForSingleObject(events[idx], INFINITE);</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (TRUE) {</div><div class="line"></div><div class="line">    <span class="keywordflow">while</span> (<a class="code" href="group___c_a_n.html#ga157d49a9343dea68ac953145e02266d8">canRead</a>(hnd, NULL, NULL, NULL, NULL, NULL) == <a class="code" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a49743d0d438957118b9c6af2e831b209">canOK</a>) {</div><div class="line">      <span class="keywordflow">if</span> (Verbose &gt; 1) printf(<span class="stringliteral">&quot;x&quot;</span>);</div><div class="line">      TotalReceived[idx]++;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (TimeToLeave) <span class="keywordflow">break</span>;</div><div class="line">    Sleep(C_NAP);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">//</span></div><div class="line"><span class="comment"></span><span class="comment">//</span></div><div class="line"><span class="keywordtype">void</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">  <span class="keywordtype">int</span> i;</div><div class="line">  <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> tid;</div><div class="line">  <a class="code" href="kvmlib_8h.html#aa8c0374618b33785ccb02f74bcfebc46">HANDLE</a> t1, t2, t3, t4;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (i=0; i&lt;4; i++) channels[i] = i;</div><div class="line"></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="comment">// First, parse the command-line arguments.</span></div><div class="line">  <span class="comment">//</span></div><div class="line">  <span class="keywordflow">for</span> (i=1; i&lt;argc; i++) {</div><div class="line">    <span class="keywordtype">int</span> tmp;</div><div class="line">    <span class="keywordtype">char</span> c;</div><div class="line">    <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-a%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      channels[0] = tmp;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-b%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      channels[1] = tmp;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-c%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      channels[2] = tmp;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-d%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      channels[3] = tmp;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-B%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      <span class="keywordflow">switch</span> (tmp) {</div><div class="line">      <span class="keywordflow">case</span> 1000 : Bitrate = <a class="code" href="canlib_8h.html#ac823cae7d94763607a7ca40fcdb5196d">canBITRATE_1M</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 500  : Bitrate = <a class="code" href="canlib_8h.html#ae72ee3c12379c4817de3b53c7cc6d89c">canBITRATE_500K</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 250  : Bitrate = <a class="code" href="canlib_8h.html#a87f618db13ec4f48610def72cfb40969">canBITRATE_250K</a>; <span class="keywordflow">break</span>;</div><div class="line">      <span class="keywordflow">case</span> 125  : Bitrate = <a class="code" href="canlib_8h.html#a29a9aafde6e861b4fffb72ad124aeed1">canBITRATE_125K</a>; <span class="keywordflow">break</span>;</div><div class="line">      default : Usage(argc, argv);</div><div class="line">      }</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (strcmp(argv[i], <span class="stringliteral">&quot;-v&quot;</span>) == 0) {</div><div class="line">      Verbose++;</div><div class="line">    } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (sscanf(argv[i], <span class="stringliteral">&quot;-L%d%c&quot;</span>, &amp;tmp, &amp;c) == 1) {</div><div class="line">      MaxLoops = tmp;</div><div class="line">    } <span class="keywordflow">else</span> {</div><div class="line">      Usage(argc, argv);</div><div class="line">    }</div><div class="line">  }</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;\nCANLIB Thread Test 4\n&quot;</span>);</div><div class="line">  memset(TotalSent, 0, <span class="keyword">sizeof</span>(TotalSent));</div><div class="line">  memset(TotalReceived, 0, <span class="keyword">sizeof</span>(TotalReceived));</div><div class="line"></div><div class="line">  <a class="code" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary</a>();</div><div class="line"></div><div class="line">  InitializeCriticalSection(&amp;cs);</div><div class="line">  TimeToLeave = FALSE;</div><div class="line"></div><div class="line">  t1 = t2 = t3 = t4 = NULL;</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span> (channels[0] != 99) t1 = CreateThread(NULL, 0, SendAndReceive, (LPVOID)0, CREATE_SUSPENDED, &amp;tid);</div><div class="line">  <span class="keywordflow">if</span> (channels[1] != 99) t2 = CreateThread(NULL, 0, SendAndReceive, (LPVOID)1, CREATE_SUSPENDED, &amp;tid);</div><div class="line">  <span class="keywordflow">if</span> (channels[2] != 99) t3 = CreateThread(NULL, 0, ReadOne, (LPVOID)2, CREATE_SUSPENDED, &amp;tid);</div><div class="line">  <span class="keywordflow">if</span> (channels[3] != 99) t4 = CreateThread(NULL, 0, Sink, (LPVOID)3, CREATE_SUSPENDED, &amp;tid);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;\n&quot;</span>);</div><div class="line">  <span class="keywordflow">if</span> (t1) printf(<span class="stringliteral">&quot;SendAndReceive on channel %d\n&quot;</span>, channels[0]);</div><div class="line">  <span class="keywordflow">if</span> (t2) printf(<span class="stringliteral">&quot;SendAndReceive on channel %d\n&quot;</span>, channels[1]);</div><div class="line">  <span class="keywordflow">if</span> (t3) printf(<span class="stringliteral">&quot;ReadOne on channel %d\n&quot;</span>, channels[2]);</div><div class="line">  <span class="keywordflow">if</span> (t4) printf(<span class="stringliteral">&quot;Sink on channel %d\n&quot;</span>, channels[3]);</div><div class="line">  printf(<span class="stringliteral">&quot;\n&quot;</span>);</div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (i=0; i&lt;4; i++) events[i] = CreateEvent(0, FALSE, FALSE, 0);</div><div class="line"></div><div class="line">  ResumeThread(t1);</div><div class="line">  ResumeThread(t2);</div><div class="line">  ResumeThread(t3);</div><div class="line">  ResumeThread(t4);</div><div class="line"></div><div class="line">  Sleep(100);</div><div class="line">  <span class="keywordflow">for</span> (i=0; i&lt;4; i++) SetEvent(events[i]);</div><div class="line"></div><div class="line">  <span class="keywordflow">while</span> (TRUE) {</div><div class="line">    Sleep(200);</div><div class="line">    <span class="keywordflow">if</span> (_kbhit()) {</div><div class="line">      _getch();</div><div class="line">      <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span> (--MaxLoops == 0) <span class="keywordflow">break</span>;</div><div class="line">  }</div><div class="line"></div><div class="line">  TimeToLeave = TRUE;</div><div class="line">  WaitForSingleObject(t1, 2000);</div><div class="line">  WaitForSingleObject(t2, 2000);</div><div class="line">  WaitForSingleObject(t3, 2000);</div><div class="line">  WaitForSingleObject(t4, 2000);</div><div class="line"></div><div class="line">  printf(<span class="stringliteral">&quot;\nChannel      Sent     Received\n&quot;</span>);</div><div class="line">  <span class="keywordflow">for</span> (i=0; i&lt;4; i++) {</div><div class="line">    printf(<span class="stringliteral">&quot;%7d%10ld%13ld\n&quot;</span>, channels[i], TotalSent[i], TotalReceived[i]);</div><div class="line">  }</div><div class="line"></div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_canlib.html">CAN bus API (CANlib)</a></li><li class="navelem"><a class="el" href="page_user_guide_canlib_samples.html">Sample Programs (CANlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
